package mtons.cast.modules.play.maker.provider;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import mtons.cast.modules.log.Printer;
import mtons.cast.modules.play.maker.Maker;
import mtons.cast.modules.utils.HttpUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author langhsu
 */
public class B365Provider implements Maker {
    HttpUtils httpUtils = new HttpUtils();

    public boolean login(String username, String password, String code) {
        try {
            httpUtils.get("http://999.cc88866.cc/Mobile_Login#login");

            Map<String, String> params = new HashMap<>();
            params.put("login", username);
            params.put("pass", password);
            params.put("authnum", code);

            String entity = httpUtils.post("http://999.cc88866.cc/Mobile_Login/Login", params);

            if (JSONObject.parseObject(entity).getInteger("code") == 200) {
                Printer.info("登录成功");

                return true;
            } else {
                Printer.info("登录失败,程序退出");
            }

        } catch (Exception e) {
            Printer.info(e.getMessage());
        }
        return false;
    }

    public String getCode() {
        String path = null;
        try {
            String root = this.getClass().getClassLoader().getResource("/").getPath();
            path = "static/upload/" + DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".png";
            File file = new File(root + path);

            if (!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            }

            httpUtils.download("http://999.cc88866.cc/Mobile_Login/authnum", new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/" + path;
    }

    public boolean doBet(String lottyid,String data,String adv) {
        String url = "http://999.cc88866.cc/DoBets/index/37";

        Map<String, String> params = new HashMap<String, String>();
        params.put("lottyid", lottyid);
        params.put("data", data);
        params.put("adv", adv);

        try {
            httpUtils.post(url, params);
            Printer.info("下单成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String getAdv() {
        try {
            String entity = httpUtils.get("http://999.cc88866.cc/DoBets/__Advisory");
            Object data = JSONObject.parseObject(entity).get("data");
            String adv = JSONObject.parseObject(data.toString()).get("adv").toString();
            return adv;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String[] getCountdown() {
        try {
            String entity = httpUtils.get("http://999.cc88866.cc/LobbyOp/getCountdown/37");

            Object data = JSONObject.parseObject(entity).get("data");
            Object now = JSONObject.parseObject(data.toString()).get("now");
            //Object next = JSONObject.parseObject(data.toString()).get("next");
            String lottyid = JSONObject.parseObject(now.toString()).get("lottyid").toString();
            String round = JSONObject.parseObject(now.toString()).get("round").toString();

            Printer.info("第"+round+"期, " + lottyid);
            return new String[]{round, lottyid};
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Double balance() {
        try {
            String entity = httpUtils.get("http://999.cc88866.cc/Common/getUserBalance");

            Object data = JSONObject.parseObject(entity).get("data");
            String balance = JSONObject.parseObject(data.toString()).get("balance").toString();
            return Double.parseDouble(balance);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Map<Integer, JSONArray> getHistory() {
        String url = "http://999.cc88866.cc/LottyOp/getResultByGTid/12/1?_=1507792696426";

        Map<Integer, JSONArray> logs = new HashMap<>();

        try {
            String result = httpUtils.get(url);

            JSONObject data = JSON.parseObject(result).getJSONObject("data");
            JSONArray details = data.getJSONArray("details");

            Iterator<Object> it = details.iterator();

            while (it.hasNext()) {
                JSONObject ob = (JSONObject) it.next();
                String round = ob.getString("round").substring(8);
                logs.put(Integer.parseInt(round), ob.getJSONArray("numbers"));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return logs;
    }

}
